package edu.guigu.serverservice.remoteaidl;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

// AIDL 实现跨进程(IPC)通信
public class AidlService extends Service {

    private MyBinder myBinder;

    public AidlService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d("TAG", getClass().getSimpleName() + " onCreate: ");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        myBinder = null;
        Log.d("TAG", getClass().getSimpleName() + " onDestroy: ");
    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.d("TAG", getClass().getSimpleName() + " onBind: ");
        if (myBinder == null) {
            myBinder = new MyBinder();
        }
        return myBinder;
    }

    // 创建Binder子类
    // @see IRemoteAidl.Stub是Binder的子类
    // AIDL接口的业务实现类
    public class MyBinder extends IRemoteAidl.Stub {
        @Override
        public void action(int type) throws RemoteException {
            Log.d("TAG", getClass().getSimpleName() + " action: " + type);
            switch (type){
                case 1:
                    action_1();
                    break;
                case 2:
                    action_2();
                    break;
                case 3:
                    action_3();
                    break;
            }
        }
        @Override
        public Student getStudent(int type) throws RemoteException {
            if (type == 1){
                return new Student("张三","三年级");
            }
            if (type == 2){
                return new Student("李四","四年级");
            }
            return new Student("赵六","八年级");
        }
    }


    public void action_1() {
        Log.d("TAG", getClass().getSimpleName() + " action_1: 执行！");
    }

    public void action_2() {
        Log.d("TAG", getClass().getSimpleName() + " action_2: 执行！");
    }

    public void action_3() {
        Log.d("TAG", getClass().getSimpleName() + " action_3: 执行！");
    }


}